/**
 * Created by huangzy on 15-6-10.
 */
var ImgUploador = RichBase.extend({
	statics : {
		ltie9 : false
	},
	EVENTS : {
		"change" : {
			"input.fileInp" : "onFileInpChange"
		}
	},
	init : function(opt){
		var that = this;
		var container = this.container = opt.container;
		var containerId = container.attr("id");
		var form = this.form = container.find("form");
		var label = this.label = container.find("label");
		var fileInp = this.fileInp = container.find("input[type=file]");
		var loading = this.loading = container.find(".loading");
		this.submitBtn = container.find(".ie8formsubmitBtn");
		//ie8及以下做fix
		if(!$.support.leadingWhitespace){
			container.hover(function(){
				label.css({"backgroundColor":"#000000", opacity:0.8})
			},function(){
				label.css({"backgroundColor":"transparent", opacity:0})
			})
		}

	},
	onFileInpChange : function(that,e){
		that.onload();
		that.form.submit();
	},
	oncomplete : function(res){
		if(res.status=="ok" && res.src){
			this.onsuccess(res);
		}else{
			this.onfail(res);
		}
		this.loading.hide();
		this.fire("oncomplete",res);
	},
	onload : function(){
		this.loading.show();
	},
	onsuccess : function(res){
		this.container.css("backgroundImage","url("+res.src+")").children(".imgurl").val(res.src);
		PFT_GLOBAL.U.Alert("success",'<p style="width:160px;">上传图片成功</p>');
	},
	onfail : function(res){
		var msg = res.msg || "上传图片失败,请稍后重试";
		PFT_GLOBAL.U.Alert("fail",'<p style="width:220px;">'+msg+'</p>');
	}
});